<template>
  <el-container>
    <!-- 左侧菜单 -->
    <el-aside width="200px" v-show="isCollapse">
      <div
        style="
          background-color: transparent;
          line-height: 35px;
          padding-top: 10px;
          width: 200px;
          height: 100px;
        "
      >
        <img
          class="img1"
          src="https://s02.mifile.cn/assets/static/image/logo-mi2.png"
          alt=""
          style="width: 60px; height: 60px"
        />
      </div>
      <el-row class="tac">
        <el-col :span="24">
          <el-menu
            :default-active="default_active"
            class="el-menu-vertical-demo"
            background-color="transparent"
            @open="handleOpen"
            @close="handleClose"
            router
            active-text-color="#409EFF"
          >
            <el-menu-item index="/admin/dashboard">
              <i class="el-icon-s-home" style="font-size: 24px"></i>
              <span slot="title" style="font-size: 20px"
                >首&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;页
              </span>
            </el-menu-item>
            <el-menu-item index="/admin/user">
              <i class="el-icon-s-custom" style="font-size: 24px"></i>
              <span slot="title" style="font-size: 20px">用户管理</span>
            </el-menu-item>
            <el-menu-item index="/admin/goods">
              <i class="el-icon-s-claim" style="font-size: 24px"></i>
              <span slot="title" style="font-size: 20px">商品管理</span>
            </el-menu-item>
            <el-menu-item index="/admin/statistics">
              <i class="el-icon-s-data" style="font-size: 24px"></i>
              <span slot="title" style="font-size: 20px">数据统计</span>
            </el-menu-item>
          </el-menu>
        </el-col>
      </el-row>
    </el-aside>
    <!-- 右侧 -->
    <el-container>
      <!-- 头部 -->
      <el-header>
        <div>
          <i
            class="el-icon-s-fold spani"
            style="font-size: 24px; cursor: pointer; margin-top: 15px"
            @click="isCollapse = !isCollapse"
          >
          </i>
          <span>
            欢迎<span class="sp1"
              >【<span class="sp2">{{ uname }}</span
              >】</span
            >登录!
          </span>
        </div>
        <!-- 全屏及退出全屏按钮 -->
        <div style="position: relative">
          <span
            :class="['iconfont', screen]"
            style="
              font-size: 26px;
              position: absolute;
              top: 10px;
              left: -60px;
              cursor: pointer;
            "
            @click="changeFull"
          ></span>
          <el-button
            style="color: white; background: #6ad2f7; margin-top: 20px"
            @click="logout"
            >退出</el-button
          >
        </div>
      </el-header>
      <!-- 页面功能 -->
      <el-main><router-view></router-view></el-main>
      <!-- 底部 -->
      <el-footer
        ><a href="https://www.mi.com/" target="_blank">了解小米</a></el-footer
      >
    </el-container>
  </el-container>
</template>

<script>
import '@/assets/font/iconfont.css'
import { tip } from '@/tools/function'
import req from '@/http/req'
export default {
  data() {
    return {
      uname: localStorage.getItem('username'),
      default_active: this.$route.fullPath,
      isCollapse: true,
      isFull: false
    }
  },
  computed: {
    // 依赖某个值的变化来进行改变
    screen() {
      return this.isFull ? 'icon-tuichuquanping' : 'icon-quanping'
    }
  },
  updated() {
    this.default_active = this.$route.fullPath
  },
  //组件生命周期   组件创建前
  // beforeCreate(){
  //   if(!localStorage.getItem('token')){
  //     this.$tip('请先登录','error')
  //     this.$router.replace('/login')
  //   } else {
  //     this.$http.get('http://10.70.30.40:5000/api/v1/profile').then((res) => {
  //       if (res.data.code === 401) {
  //         this.$tip('请先登录','error')
  //         this.$router.replace('/login')
  //       }else{
  //         localStorage.setItem('userInfo',JSON.stringify(res.data.data))
  //       }
  //     })
  //   }
  // },

  //组件内路由前置守卫
  // beforeRouteEnter(to,from,next){
  //   if(!localStorage.getItem('token')){
  //     tip('请先登录','error')
  //     next('/login')
  //   } else {
  //     req.get('http://10.70.30.40:5000/api/v1/profile').then((res) => {
  //       if (res.data.code === 401) {
  //         tip('请先登录','error')
  //         next('/login')
  //       }else{
  //         localStorage.setItem('userInfo',JSON.stringify(res.data.data))
  //       }
  //     })
  //   }
  // },
  methods: {
    changeFull() {
      // 如果是全屏状态 就需要退出全屏
      if (this.isFull) {
        if (document.exitFullScreen) {
          document.exitFullscreen()
        }
        //兼容火狐
        console.log(document.mozExitFullScreen)
        if (document.mozCancelFullScreen) {
          document.mozCancelFullScreen()
        }
        //兼容谷歌等
        if (document.webkitExitFullscreen) {
          document.webkitExitFullscreen()
        }
        //兼容IE
        if (document.msExitFullscreen) {
          document.msExitFullscreen()
        }
        this.isFull = !this.isFull
      } else {
        if (document.documentElement.RequestFullScreen) {
          document.documentElement.RequestFullScreen()
        }
        //兼容火狐
        console.log(document.documentElement.mozRequestFullScreen)
        if (document.documentElement.mozRequestFullScreen) {
          document.documentElement.mozRequestFullScreen()
        }
        //兼容谷歌等可以webkitRequestFullScreen也可以webkitRequestFullscreen
        if (document.documentElement.webkitRequestFullScreen) {
          document.documentElement.webkitRequestFullScreen()
        }
        //兼容IE,只能写msRequestFullscreen
        if (document.documentElement.msRequestFullscreen) {
          document.documentElement.msRequestFullscreen()
        }
        this.isFull = !this.isFull
      }
    },
    logout() {
      this.$tip('注销成功', 'warning')
      localStorage.removeItem('token')
      this.$router.replace('/login')
    },
    handleOpen(key, keyPath) {
      // console.log(key, keyPath);
    },
    handleClose(key, keyPath) {
      // console.log(key, keyPath);
    }
  }
}
</script>

<style lang="scss" scoped>
.el-container {
  height: 100vh;
}
.el-aside {
  /* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */
  color: #333;
  text-align: center;
  line-height: 200px;
}
.el-footer {
  background-color: transparent;
  color: #333;
  text-align: center;
  line-height: 60px;
}
.el-header {
  background-color: transparent;
  color: #333;
  text-align: left;
  line-height: 60px;
  display: flex;
  justify-content: space-between;
  .sp1 {
    font-size: 30px;
    font-weight: bold;
    .sp2 {
      font-size: 40px;
    }
  }
}
.el-main {
  background: transparent;
  margin: 15px 15px;
  color: #333;
  text-align: center;
  // line-height: 160px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}
.el-menu {
  border: 0;
}
.el-menu-item {
  text-align: left;
}
:deep(.is-active) {
  color: #409eff;
}
.el-menu-item:hover {
  background-color: transparent !important;
}
a {
  text-decoration: none;
  color: #333;
  font-size: 20px;
}
:deep(.img1) {
  margin-left: -90px;
  margin-top: 15px;
}
</style>